//创建action
import *as actionTypes from './constants'
//获取请求的结果并通过这个结果创建对应的action
import { getTopBanner, getHotRecommends, getNewAlbums, getTopList } from '@/netWork/recommend'

//轮播图的action
//通过下面返回的res创建action
const changeTopBannerAction = (res) => {
    return {
        type: actionTypes.CHANGE_TOP_BANNERS,
        topBanners: res.banners
    }
}
export const getTopBannerAction = (data) => {
    return dispatch => {
        getTopBanner().then((res) => {
            dispatch(changeTopBannerAction(res))
        })
    }
}

// 热门推荐的action
const changeHotRecommendsAction = (res) => {
    return {
        type: actionTypes.CHANGE_HOT_RECOMMEND,
        hotRecommends: res.result
    }
}
export const getHotRecommendsAction = (limit) => {
    return dispatch => {
        getHotRecommends(limit).then(res => {
            dispatch(changeHotRecommendsAction(res))
        })
    }
}

//获取新碟上映数据
const changeNewAlbumAction = (res) => {
    return {
        type: actionTypes.CHANGE_NEW_ALBUM,
        newAlbums: res.albums
    }
}
export const getNewAlbumAction = (limit) => {
    return dispatch => {
        getNewAlbums(limit).then(res => {
            dispatch(changeNewAlbumAction(res))
        })
    }
}
//获取榜单数据
// 飙升榜
const changeUpRanking = res => {
    return {
        type: actionTypes.CHANGE_UP_RANKING,
        upRanking: res.playlist
    }
}
// 新歌榜
const changeNewRanking = res => {
    return {
        type: actionTypes.CHANGE_NEW_RANKING,
        newRanking: res.playlist
    }
}
//原创榜
const changeOriginalRanking = res => {
    return {
        type: actionTypes.CHANGE_ORIFINAl_RANKING,
        originalRanking: res.playlist
    }
}
export const getTopListAction = idx => {
    return dispatch => {
        if (idx === 0) {
            getTopList(idx).then(res => {
                dispatch(changeNewRanking(res))
            })
        } else if (idx === 2) {
            getTopList(idx).then(res => {
                dispatch(changeOriginalRanking(res))
            })
        } else {
            getTopList(idx).then(res => {
                dispatch(changeUpRanking(res))
            })
        }

    }
}